﻿using System;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.Data.SqlClient;
using System.Web.Configuration;
using System.Drawing;
using System.IO;

namespace Perm.Web
{
    [ServiceContract(Namespace = "")]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class Banner
    {
        SqlConnection conMSSQL;
        SqlDataReader readerMSSQL;
        SqlCommand commandMSSQL;

        DateTime thisDate = DateTime.Now.ToUniversalTime().AddHours(6);
        string addCity = "", error = null, String = "";

        [OperationContract]
        public void DoWork(Stream stream)
        {
            string adress = "D:/users/yakimov-1/yakimov-10/WWWRoot/banner" + addCity + ".png";
            string adress2 = "D:/users/yakimov-1/yakimov-14/WWWRoot/raiting/banner" + addCity + ".png";
            string adressLOCAL = "h:/banner" + addCity + ".png";
            try
            {
                Image img = Image.FromStream(stream);
                img.Save(adress);
                img.Save(adress2);
            }
            catch { error += " Невозможно сохранить баннер."; }

            string connectionStringMSSQL = WebConfigurationManager.ConnectionStrings["MSSQL"].ConnectionString;
                try
                {
                    conMSSQL = new SqlConnection(connectionStringMSSQL);
                    conMSSQL.Open();
                }
                catch { error += " Невозможно подключиться к базе данных. MS SQL (баннер)"; }

            if (error != null)
                {
                    String = "INSERT INTO ERRORS (DATA, ERROR) VALUES ('" + thisDate + "', '" + error + "')";
                                            
                    try
                    {
                        commandMSSQL = new SqlCommand(String, conMSSQL);
                        commandMSSQL.ExecuteNonQuery();
                    }
                    catch { }
                }
            conMSSQL.Close();
            return;
        }

        // Add more operations here and mark them with [OperationContract]
    }
}
